import formJs from "./regexDetail.js"; //新增弹框配置项
import TableData from "@/components/public/el-table"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyout.vue'
import messageBox from "@/components/public/messageBox"; //主页面分页

import {
    getWbs, //WBSWBS
    getTokens,
    getParts
} from "@/api/request.js";

export default {
    props: [
        "dialogVisible",
        "token"
    ],
    data() {
        return {
            showMsgBox: false,
            // 页签切换
            typeData: [{
                    text: "分部分项",
                    show: true
                },
                {
                    text: "部位查询",
                    show: false
                },
            ],
            msgTip: "请先选中数据行！",
            msgType: 1,
            visible: "",
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            materialInfo_config: formJs.materialInfo_config,
            tableData_Wbs: [],

            loading: false,
            loadingPart: false,

            materialData: [], //种类数据
            tableData: [],
            title: null,
            isLock: false,
            dialogHeight: null,
            activeIndex: "1",
            btnShow: true,
            WbsRow: [],
            WbsRow1: [],
            WbsRow2: [],

            types: null,
            primaryInfo: null,
            getList: {
                secId: "", //标段Id（高信的）
                unitKey: "", //单位工程关键字
                partKey: "", //具体部位关键字
                pageIndex: 1,
                pageSize: 10,
            },
            getLists: {
                secId: "", //标段Id（高信的）

            },
            tendersObj: [],
            selectText: null,
            queryCriteriaData: null,
            iframeUrl:null
            // currentPage4: 1

        };
    },
    computed: {
        hIndex() {
            return (this.getList.pageIndex - 1) * this.getList.pageSize + 1;
        },
    },
    components: {
        // publicForm,
        TableLyout,
        TableData,
        messageBox
        // publicForm1
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        console.log(999, this.primaryInfo);

        this.btnShow = true
        this.getToken();
        this.getHeight();
    },
    methods: {
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChanges(val) {
            this.getList.pageIndex = val;
            this.getParts(this.getList)

            // this.
        },
        // 页签切换
        changeType(e, index) {
            for (let i in this.typeData) {
                if (parseFloat(i) === index) {
                    this.typeData[i].show = true;
                } else {
                    this.typeData[i].show = false;
                }
            }
            // this.typeDataNum = index;
        },
        async getTender(e) {
            // this.iframeUrl= window.location.href;;
            // this.iframeUrl="http://172.18.5.31:8301/inspectionManagementManage/inspectionReport?token=Bearer%20eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzE0Nzc5MDUsInVzZXJfbmFtZSI6IkpMQkQxIiwianRpIjoiN2MzNmRhZjctNzc2My00YjI4LWIwOTUtODZhOTUxMWE4YmRkIiwiY2xpZW50X2lkIjoiZGV2LWdhdGV3YXkiLCJzY29wZSI6WyJhbGwiXX0.U697kmaxQPcwtl0l76DszoR-SPTKOr9i0Gi4fpVGJ5j6kH2d6IoWCV6nimrMBm-HePcg1bGlYSCpIbmIN7glbXQHQ6LZ9isc7OrX1CXzLGkGRI-B8I1YL6sTE3hYzthGzTzflH3AhJYwVFVTaAhW7HqrSL-E2QRJ6kACwAT7O80LXOSb9dXmS9lW--fHdIcyba6zM91RWS3eisGypXCpksBIu-p0SEqUnNHMo9Xo79WHn4ZbLghjGcs7fVunkPWpo392GxuJwpLwSO4x0jOr90YUWo_ByJcEMpcUxUecKChpangxRvF7grB7hcG0YFBUIhZVw9s7MO14N9A1Fb8CGw"

            // var token =  this.iframeUrl.split("=")[1]
            //       var reg = new RegExp("Bearer%20")
            //       console.log( "'Bearer '+token.replace(reg, '')",this.iframeUrl, 'Bearer '+token.replace(reg, ""));
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                    headers: {
                        "Content-Type": "application/json",
                        Authorization: this.token,
                    },
                }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.tendersObj = [];

                    if (data.length == 1) {
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.gxCompanyId,
                            });
                        });

                        this.getLists.secId = data[0].gxCompanyId;
                        this.getList.secId = data[0].gxCompanyId;

                        this.yj_searchForm(this.getLists.secId, "");
                        //   this.getParts(this.getList)
                    } else {
                        this.tendersObj = [{
                            fItemName: "所有标段",
                            fItemValue: "",
                        }, ];
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.gxCompanyId,
                            });
                        });
                        //   his.yj_searchForm(this.getLists.secId, "");
                    }
                }
            });
        },
        selectCompanyid(e) {
            console.log(e);
        },
        confirmMsg() {
            this.showMsgBox = false;
        },
        // 带功能确定
        confirmMsgs() {
            this.showMsgBox = false;
            if (this.titelName == "提交") {
                this.yj_submit();
            } else if (this.titelName == "删除") {
                this.yj_delete();
            } else if (this.titelName == "撤销") {
                this.yj_revoke();
            }
        },


        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialog1() {
            this.$emit("close1");
        },
        getParts(data) {
            this.loadingPart = true;
            getParts(data)
                .then((res) => {
                    if (res.Data.Result == 100) {
                        this.loadingPart = false;
                        res.Data.Data.List.map(item => {
                            if (item.IsLeaf == 1) {
                                item.IsLeaf = 0;
                            }
                        })
                        this.tableData = res.Data.Data.List;
                        this.queryCriteriaData = res.Data.Data.Total;

                    } else {
                        this.$message.error({
                            content: res.Data.Msg,
                            duration: 1000
                        })
                        this.loadingPart = false;

                    }
                })
        },

        // WBS
        async getToken() {
            getTokens({
                accId: "289480312365111", //访问Id
                accSecret: "46C3F504CA904BFF81EEED4E14D12FC1" //访问秘钥
            }).then((res) => {
                console.log(res.Data.Result);
                if (res.Data.Result == 100) {
                    sessionStorage.setItem("GxEmbedded_token1", res.Data.Data);
                    this.getTender(this.primaryInfo.fprojectid)
                } else {
                    this.$message.error({
                        content: res.Data.Msg,
                        duration: 1000
                    })
                }
            })
            // await this.$http(
            //     "https://xayj.limsapp.cn:8500/IOTManagement/roadApi/getToken.do"
            // ).then((res) => {
            //     if (res.status == 200) {
            //         // window.sessionStorage.setItem("GxBack-token", JSON.stringify(token));
            //         sessionStorage.setItem("GxEmbedded-token", res.data.data.Authorizations);
            //         this.yj_searchForm(this.primaryInfo.fCompanyid, "");

            //     }
            // });
            // this.yj_searchForm();

        },
        // 获取列表数据
        yj_searchForm(id, num) {

            this.visible = true;
            this.load = true;


            getWbs({
                    // secId:id,
                    secId: this.getLists.secId,
                    nodeId: ""
                    // fCompanyid: "5e8ce27d-fbd7-40ba-962c-e7869e3f3b7f",
                    // fCompanyid: id, //标段id 必填
                    // projectId: "1666696366155796480", //项目id 必填

                    // projectId: this.primaryInfo.fprojectid, //项目id 必填
                    // parentId: "-1" //父级id 第一级传-1  子级取上一级fId或fNodeid 必填
                })
                .then((res) => {
                    if (res.Data.Result == 100) {
                        res.Data.Data.map(item => {
                            if (item.IsLeaf == 0) {
                                item.IsLeaf = 1;
                            }
                        })
                        this.tableData_Wbs = res.Data.Data;
                        this.loading = false;
                    } else {
                        this.$message.error({
                            content: res.Data.Msg,
                            duration: 1000
                        })
                        this.loading = false;
                    }
                })
        },
        treeLoad(tree, treeNode, resolve) {
            this.load = true;
            getWbs({
                    // secId:this.primaryInfo.fCompanyid,
                    secId: this.getLists.secId,
                    nodeId: tree.NodeId
                    // fCompanyid: tree.fCompanyid, //标段id 必填
                    // projectId: "1666696366155796480", //项目id 必填
                    // projectId: this.primaryInfo.getList.fprojectid, //项目id 必填
                    // parentId: tree.fId //父级id 第一级传-1  子级取上一级fId或fNodeid 必填
                })
                .then((res) => {
                    if (res.Data.Result == 100) {
                        this.loading = false;
                        res.Data.Data.map(item => {
                            if (item.IsLeaf == 0) {
                                item.IsLeaf = 1;
                            }

                        })
                        return resolve(res.Data.Data);
                    } else {
                        this.loading = false;
                        this.$message.error({
                            content: res.Data.Msg,
                            duration: 1000
                        })
                    }
                })

        },
        // 点击当前行变色
        handleCurrentChange(val) {
            // console.log(val);
            // this.WbsRow = val;
        },
        handleSelectionChange(val) {
            // console.log(val);



            // this.formData.engineeringId = id.join(','); //工程部位id
            this.WbsRow1 = val;
            this.WbsRow = [...this.WbsRow1, ...this.WbsRow2];

            var name = [];
            // var id = [];
            this.WbsRow.map(item => {
                name.push(item.ShortName);
                // id.push(item.LongIds);
            })
            // console.log(data, name, id);
            this.selectText = name.join(','); //工程部位
        },

        handleSelectionChange1(val) {
            this.WbsRow2 = val;
            this.WbsRow = [...this.WbsRow1, ...this.WbsRow2];

            var name = [];
            // var id = [];
            this.WbsRow.map(item => {
                name.push(item.ShortName);
                // id.push(item.LongIds);
            })
            // console.log(data, name, id);
            this.selectText = name.join(','); //工程部位
        },
        // 行颜色
        tableRowClassName({
            row
        }) {
            if (
                (row.fParentid == 0 && !row.children) ||
                (row.fParentid == null && !row.children)
            ) {
                return "fristColor";
            }
        },
        yj_saveForm() {

            if (this.WbsRow.length != 0) {
                this.$emit('getWBS', this.WbsRow)
                this.visible = false
            } else {
                this.$message.error({
                    content: '请先选择工程部位',
                    duration: 1000
                })

            }
        },

    },
};